package com.bankcomm.ui.show;

import com.bankcomm.BankcommApplication;
import com.bankcomm.R;
import com.bankcomm.ui.BaseViewCopy;
import com.rytong.tools.utils.Utils;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SwitchViewActivity extends BaseViewCopy implements
		OnViewChangeListener, OnClickListener {
	/** Called when the activity is first created. */

	public MyScrollLayout mScrollLayout;

	private ImageView[] mImageViews;

	private int mViewCount;

	private int mCurSel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.swichview);

		init();
	}

	private void init() {
		mScrollLayout = (MyScrollLayout) findViewById(R.id.ScrollLayout);

		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.llayout);

		mViewCount = mScrollLayout.getChildCount();
		mImageViews = new ImageView[mViewCount];

		for (int i = 0; i < mViewCount; i++) {
			mImageViews[i] = (ImageView) linearLayout.getChildAt(i);
			mImageViews[i].setEnabled(true);
			mImageViews[i].setOnClickListener(this);
			mImageViews[i].setTag(i);
		}

		mCurSel = 0;
		mImageViews[mCurSel].setEnabled(false);
		mScrollLayout.SetOnViewChangeListener(this);

	}

	private void setCurPoint(int index) {
		if (index < 0 || index > mViewCount - 1 || mCurSel == index) {
			return;
		}

		mImageViews[mCurSel].setEnabled(true);
		mImageViews[index].setEnabled(false);

		mCurSel = index;
	}

	@Override
	public void OnViewChange(int view) {
		// TODO Auto-generated method stub
		setCurPoint(view);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int pos = (Integer) (v.getTag());
		setCurPoint(pos);
		mScrollLayout.snapToScreen(pos);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		try{
			if(null != mScrollLayout && null != mImageViews){
				int size = mScrollLayout.getChildCount();
				for(int i = 0 ;i < size;i++){
					ImageView view = (ImageView) mScrollLayout.getChildAt(i);
					Utils.releaseObject(view);
					Utils.releaseObject(mImageViews[i]);
					System.gc();
				}
				mScrollLayout = null;
			}
		}catch(RuntimeException e){
			e.printStackTrace();
		}
		BankcommApplication.getInstance().removeActivity(this);
	}
}